/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Settings;
using MyPhotoIndex.Plugins;
using MyPhotoIndex.Interfaces;
using System.Collections.ObjectModel;
using System.Drawing.Printing;
using System.Threading;
using MyPhotoIndex.Interfaces.Plugins;
using MyPhotoIndex.Interfaces.Imaging;
using MyPhotoIndex.Utilities;

namespace MyPhotoIndex
{
    public partial class PrintForm : FormTemplate
    {
        private delegate void VoidDelegate();

        static List<RadioButton> m_rbList;

        private IPrintPlugin                    m_selectedPlugin;
        private ReadOnlyCollection<IImageData>  m_imageList;
        private Image m_currentPreview;

        public IPrintPlugin SelectedPlugin
        {
            get { return m_selectedPlugin; }
            set { m_selectedPlugin = value; }
        }

        public PrintForm(ReadOnlyCollection<IImageData> imageList)
        {
            InitializeComponent();
            m_imageList = imageList;
        }

        private void PrintForm_Load(object sender, EventArgs e)
        {
            ReadOnlyCollection<IPrintPlugin> printPlugins = Manager.GetPlugins<IPrintPlugin>();

            int ii = 15;
            m_rbList = new List<RadioButton>();
            foreach (IPrintPlugin plugin in printPlugins)
            {
                RadioButton button = new RadioButton();
                button.Parent = groupBox1;
                button.Tag = plugin;
                button.Text = plugin.ToString();
                button.Top = ii;
                button.Left = 5;
                button.Width = groupBox1.Width - 15;
                button.BackColor = Color.Transparent;
                button.Visible = true;
                button.CheckedChanged += new EventHandler(button_CheckedChanged);
                ii += button.Height + 5;

                m_rbList.Add(button);
            }

            bool found = false;
            String defaultPrintPlugin = ApplicationSettings.Default.DefaultPrintPlugin;
            if (String.IsNullOrEmpty(defaultPrintPlugin) == false)
            {
                foreach (RadioButton button in m_rbList)
                {
                    if (button.Tag.GetType().FullName.CompareTo(defaultPrintPlugin) == 0)
                    {
                        button.Checked = true;
                        found = true;
                        break;
                    }
                }
            }

            if (found == false)
            {
                m_rbList[0].Checked = true;
            }
        }

        void button_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked == false)
            {
                return;
            }

            IPrintPlugin plugin = (IPrintPlugin)rb.Tag;
            m_selectedPlugin = plugin;

            Thread t = new Thread(new ThreadStart(GeneratePreview));
            t.Start();
        }

        private void GeneratePreview()
        {
            try
            {
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                }

                using (PrintDocument document = m_selectedPlugin.GetPrintDocument(m_imageList))
                {
                    Rectangle bounds = document.PrinterSettings.DefaultPageSettings.Bounds;

                    Image image = null;

                    if (m_selectedPlugin.PrintLandscape == true)
                    {
                        image = new Bitmap(bounds.Height, bounds.Width);
                    }
                    else
                    {
                        image = new Bitmap(bounds.Width, bounds.Height);
                    }

                    MyPreviewPrintController ppv = new MyPreviewPrintController(image);
                    document.PrintController = ppv;

                    using (Graphics g = Graphics.FromImage(image))
                    {
                        Rectangle borderRect = Rectangle.Inflate(new Rectangle(0, 0, image.Width, image.Height), -2, -2);
                        g.SetClip(borderRect);
                        g.FillRectangle(Brushes.White, borderRect);

                        for (int ii = 0; ii < 10; ii++)
                        {
                            g.DrawRectangle(Pens.Black, borderRect);
                            borderRect = Rectangle.Inflate(borderRect, -1, -1);
                        }
                    }

                    document.Print();
                    m_currentPreview = image;

                    this.Invoke(new VoidDelegate(SetPreviewImage));
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }

        private void SetPreviewImage()
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.Image = m_currentPreview;
        }

        protected override void OkButton_Click(object sender, EventArgs e)
        {
            foreach (RadioButton button in m_rbList)
            {
                if (button.Checked == true)
                {
                    ApplicationSettings.Default.DefaultPrintPlugin = button.Tag.GetType().FullName;
                    m_selectedPlugin = (IPrintPlugin)button.Tag;
                    DynamicConfiguration.Default.Save();
                    break;
                }
            }

            base.OkButton_Click(sender, e);
        }

        private void LandscapeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
           // m_selectedPlugin.PrintLandscape = LandscapeCheckBox.Checked;
            //if (null != m_selectedPlugin.PrintSample)
            //{
            //    PreviewPictureBox.Image = m_selectedPlugin.PrintSample;
            //}
            //else
            //{
            //    PreviewPictureBox.Image = null;
            //}
        }

        private void printPreviewControl1_Paint(object sender, PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }
    }


    class MyPreviewPrintController : PrintController
    {
        private Image m_image;

        public MyPreviewPrintController(Image image)
        {
            m_image = image;
        }

        public override bool IsPreview
        {
            get
            {
                return true;
            }
        }

        public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
        {
            Graphics printGraphics = Graphics.FromImage(m_image);

            return printGraphics;
        }

    }
}